﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLCONGNOLIBS.Models;
using QLCONGNOLIBS.Securities;

namespace QLCONGNOLIBS.Controllers
{
    public class ConnectionManager
    {
        private SqlConnection m_objConnection;

        //SQLClient
        public SqlConnection SqlclientConnection(string connectString)
        {
            //1. Khởi tạo đối tượng kết nối
            SqlConnection m_objConnection = new SqlConnection(connectString);

            try
            {
                //2. Mở kết nối
                m_objConnection.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return m_objConnection;
        }

        //OleDb
        public OleDbConnection OleDbConnection(string connectString)
        {
            //1. Khởi tạo đối tượng kết nối
            OleDbConnection m_objConnection = new OleDbConnection(connectString);

            try
            {
                //2. Mở kết nối
                m_objConnection.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return m_objConnection;
        }

        public SqlConnection SqlclientConnection(ConnectObject obj)
        {
            //1. Khởi tạo đối tượng kết nối
            obj._SERVER =       SecurityConexts.DecryptInfo(obj._SERVER);
            obj._DATABASE =  SecurityConexts.DecryptInfo(obj._DATABASE);
            obj._USERNAME = SecurityConexts.DecryptInfo(obj._USERNAME);
            obj._PASSWORD = SecurityConexts.DecryptInfo(obj._PASSWORD);

            string connString = String.Format("Server={0};Database={1};UId={2};PWD={3};",
                                obj._SERVER, obj._DATABASE, obj._USERNAME, obj._PASSWORD);
            m_objConnection = new SqlConnection(connString);

            try
            {
                //2. Mở kết nối
                m_objConnection.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return m_objConnection;
        }
    }
}
